package java101;

import java.util.Scanner;

import javax.swing.JOptionPane;

public class NearstFibWithMethods {
	
	static int[] fib = new int[25];
	static {
		for (int i = 0; i < 25; i++) {
			fib[i] = i < 2? i : fib[i-2] + fib[i-1];
			//Is i < 2? If so, print i. Otherwise, print fib[i-2] + fib[i-1].
	}
	}
		//This is now available to any methods that need it.
	
	static Scanner sc = new Scanner(System.in);	
	
	public static void main(String[] args) {
		
		
		
		
		String input = JOptionPane.showInputDialog
		("Enter a number between 0 and 10,000: ");
		int n = Integer.parseInt(input);
		
		if (n < 0 || n > 10000) {
			System.out.println("You didn't enter a number between 0 and 10,000!");
			System.exit(0);
		}
		
		nearestFib(n);

	}

	public static int nearestFib(int n) {
		for (int i = 1; i < 25; i++) {
					
					if (n == fib[i] || n == fib[i-1]) {
						System.out.println("Congrats! " + n + " is a Fib number!");
						break;
					}
					
					else if (fib[i-1] < n && n < fib[i]) {
						
						int diffa = n - fib[i-1];
						
						int diffb = fib[i] - n;
						
						if (diffa < diffb) {
							System.out.println("The nearest Fib is " + fib[i-1] + ".");
							break;
						}
						
						else if (diffa == diffb) {
							System.out.println(n + " is exactly between " + fib[i-1] + " and " + fib[i] + ".");
							break;
						}
						
						else {
							System.out.println("The nearest Fib is " + fib[i] + ".");
							break;
						}
						
					}
		}
		return -1;
	}
}
